﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Einstein.Utility
{
    public class IPConvertor
    {
        public static string GetIPFromBytes(byte[] bytes)
        {
            if (bytes != null && bytes.Length > 0)
            {
                string ip=string.Empty;
                for(int i=0;i<bytes.Length;i++)
                {
                    if (string.IsNullOrEmpty(ip))
                    {
                        ip = bytes[i] + "";
                    }
                    else
                    {
                        ip = ip + "." + bytes[i];
                    }
                }
                return ip;
            }
            return string.Empty;
        }

        public static byte[] GetBytesFromIP(string ip)
        {
            if (string.IsNullOrEmpty(ip)) return null;
            string[] strs = ip.Split('.');
            if (strs.Length > 0)
            {
                List<byte> list = new List<byte>();
                for (int i = 0; i < strs.Length; i++)
                {
                    list.Add(byte.Parse(strs[i]));
                }
                return list.ToArray();
            }
            return null;
        }

    }
}
